package com.zhangyang.esper.one;

import com.espertech.esper.client.*;

/**
 * @description:
 * @author: fanxiaole
 * @date: 2022/2/14 9:38
 */
public class AppleTest {

    public static void main(String[] args) throws Exception {
        EPServiceProvider epService = EPServiceProviderManager.getDefaultProvider();
        EPAdministrator admin = epService.getEPAdministrator();

        // 指定事件模型
        String apple = Apple.class.getName();

        // 描述复杂事件
//        String epl = "select avg(price) from " + apple + ".win:length_batch(3)";
//        String epl = "select * from " + apple + ".win:length_batch(3) group by name";
//        String epl = "select * from " + apple + ".win:time(5 sec) group by name";
        String epl = "select sum(price), name, id from " + apple + ".win:time(5 sec) group by name";
//        String epl = "select * from " + apple + " group by name";

        // 添加事后处理
        EPStatement state = admin.createEPL(epl);
        state.addListener(new AppleListener());

        EPRuntime runtime = epService.getEPRuntime();

        // 模拟事件发生
        Apple apple1 = new Apple();
        apple1.setId(1);
        apple1.setPrice(5);
        apple1.setName("苹果");
        runtime.sendEvent(apple1);

        Apple apple2 = new Apple();
        apple2.setId(2);
        apple2.setPrice(2);
        apple2.setName("橘子");
        runtime.sendEvent(apple2);

        Apple apple3 = new Apple();
        apple3.setId(3);
        apple3.setPrice(9);
        apple3.setName("苹果");
        runtime.sendEvent(apple3);

        Thread.sleep(10000);

        Apple apple4 = new Apple();
        apple4.setId(4);
        apple4.setPrice(4);
        apple4.setName("香蕉");
        runtime.sendEvent(apple4);

        Apple apple5 = new Apple();
        apple5.setId(5);
        apple5.setPrice(2);
        apple5.setName("苹果");
        runtime.sendEvent(apple5);
    }

}
